package com.aisa.tracker;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.graphics.Color;
import android.widget.LinearLayout;
import android.database.Cursor;
import android.util.Log;

import java.util.Random;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.chart.TimeChart;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.achartengine.model.XYSeries;



public class GraphBasic extends Activity {

	private XYMultipleSeriesDataset getMainsDataset() {
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		mDbHelper.open();
		Cursor cursor = mDbHelper.fetchAllMains();
		int numrecs = cursor.getCount();
		for (int a = 2; a <=5; a++) { //for each of 4 attrs (col 0 = id, col 1 = timestamp)
			XYSeries series = new XYSeries("Column" + a);
			cursor.moveToFirst();
			for (int i = 0; i < numrecs; i++) { //each row
				//Log.w("DB", "adding series x (" + cursor.getColumnName(1) + ") is "+cursor.getInt(1) + ", y (" + cursor.getColumnName(a) + ") is " + cursor.getInt(a));
        series.add(cursor.getInt(1), cursor.getInt(a)); //get int at given column
				cursor.moveToNext();
			}
			dataset.addSeries(series);
    }
		mDbHelper.close();
    return dataset;
	}

	private XYMultipleSeriesRenderer getDemoRenderer() {
    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		
    XYSeriesRenderer r = new XYSeriesRenderer();
    r.setColor(Color.BLACK);
    r.setPointStyle(PointStyle.CIRCLE);
    renderer.addSeriesRenderer(r);
		
		XYSeriesRenderer s = new XYSeriesRenderer();
    s.setColor(Color.RED);
    s.setPointStyle(PointStyle.SQUARE);
    renderer.addSeriesRenderer(s);
		
		XYSeriesRenderer t = new XYSeriesRenderer();
    t.setColor(Color.YELLOW);
    t.setPointStyle(PointStyle.CIRCLE);
    renderer.addSeriesRenderer(t);
		
		XYSeriesRenderer u = new XYSeriesRenderer();
    u.setColor(Color.YELLOW);
    u.setPointStyle(PointStyle.SQUARE);
    renderer.addSeriesRenderer(u);
		
    setChartSettings(renderer);
    return renderer;
	}	
	
	private void setChartSettings(XYMultipleSeriesRenderer renderer) {
    renderer.setChartTitle("Chart demo");
    renderer.setXTitle("x values");
    renderer.setYTitle("y values");
    renderer.setApplyBackgroundColor(false);
    //renderer.setRange(new double[] {0,6,-70,40});
    //renderer.setFitLegend(false);
    renderer.setAxesColor(Color.BLACK);
    renderer.setShowGrid(true);
    renderer.setYAxisMin(5);
    renderer.setYAxisMax(5);
		//renderer.setAxisTitleTextSize(12);
    //renderer.setChartTitleTextSize(12);
    //renderer.setLabelsTextSize(15);
    //renderer.setLegendTextSize(15);
    //renderer.setPointSize(5f);
    //renderer.setMargins(new int[] { 20, 30, 15, 0 });
		//renderer.setPanEnabled(true, false);
		renderer.setZoomEnabled(true, true);
  }
	
	private LinearLayout layout;
	private DbAdapter mDbHelper;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.graph);
		mDbHelper = new DbAdapter(this);
		layout = (LinearLayout) findViewById(R.id.graph2);
		View leChart = ChartFactory.getTimeChartView(this, getMainsDataset(), getDemoRenderer(), null);
		layout.addView(leChart);
	}
}